/*
 * @Descripttion :
 * @Versions     : 0.1
 * @Author       : foxui team
 * @Date         : 2022-03-06 13:04:08
 * @LastEditors  : foxui team
 * @LastEditTime : 2022-03-06 22:37:39
 */

function Spinner() {
    this.HANDLE = '.foxui-spinner .foxui-spinner-slide';
    this.CONTAINER = '.foxui-spinner';
    this.SLIDE = '.foxui-spinner-slide';
    this.ITEM = '.foxui-spinner-item';
    this.NOT_EMPTY_ITEM = '.foxui-spinner-item:not(.empty)';
    this.INDICATOR = '.foxui-spinner-indicator';
}

Spinner.prototype._init = function () {
    this._bindScrollForChrome();
    this._bindScrollForFirefox();
    this._handleClickItem();
};

/**
 * @description: 绑定监听浏览器滚动(谷歌浏览器)
 * @param {*}
 * @return {*}
 * @Date: 2022-03-06 13:14:19
 */
Spinner.prototype._bindScrollForChrome = function () {
    let that = this;
    // $(document).on('mousewheel', this.HANDLE, function (event) {
    //     if (event.originalEvent.wheelDelta > 0) {
    //         that._mouseScroll(this, 'down');
    //     } else {
    //         that._mouseScroll(this, 'up');
    //     }
    //     // 阻止默认行为
    //     return false;
    // });
    $(this.HANDLE)
        .off('mousewheel')
        .on('mousewheel', function (event) {
            if (event.originalEvent.wheelDelta > 0) {
                that._mouseScroll(this, 'down');
            } else {
                that._mouseScroll(this, 'up');
            }
            // 阻止默认行为
            return false;
        });
};

/**
 * @description: 绑定监听浏览器滚动(火狐浏览器)
 * @param {*}
 * @return {*}
 * @Date: 2022-03-06 13:14:28
 */
Spinner.prototype._bindScrollForFirefox = function () {
    let that = this;
    // $(document).on('DOMMouseScroll', this.HANDLE, function (event) {
    //     if (event.originalEvent.detail < 0) {
    //         that._mouseScroll(this, 'down');
    //     } else {
    //         that._mouseScroll(this, 'up');
    //     }
    //     // 阻止默认行为
    //     event.preventDefault();
    // });
    $(this.HANDLE)
        .off('DOMMouseScroll')
        .on('DOMMouseScroll', function (event) {
            if (event.originalEvent.detail < 0) {
                that._mouseScroll(this, 'down');
            } else {
                that._mouseScroll(this, 'up');
            }
            // 阻止默认行为
            event.preventDefault();
        });
};

Spinner.prototype._handleClickItem = function () {
    let that = this;
    $(document).on('click', this.NOT_EMPTY_ITEM, function () {
        let isActive = $(this).is('.is-active');
        if (!isActive) {
            let $this = $(this),
                $slide = $this.closest(that.CONTAINER).find(that.SLIDE),
                eq = $slide.find(that.NOT_EMPTY_ITEM).index(this),
                lineHeight = $this.height();

            $slide.animate({ scrollTop: eq * lineHeight }, 80);
            that._adjustActive($slide, eq);
        }
    });
};

/**
 * @description: 修改鼠标滚动距离
 * @param {*} slide       .foxui-spinner-slide
 * @param {*} direction
 * @return {*}
 * @Date: 2022-03-06 13:31:39
 */
Spinner.prototype._mouseScroll = function (slide, direction) {
    let $slide = $(slide),
        y = $slide.scrollTop(),
        lineHeight = $slide.find(this.ITEM).height();

    y = Math.ceil(y);
    lineHeight = Math.round(lineHeight);

    if (direction === 'up') {
        $slide.scrollTop(y + lineHeight);
    } else if (direction === 'down') {
        $slide.scrollTop(y - lineHeight);
    }
    let eq = $slide.scrollTop() / lineHeight;

    this._adjustActive(slide, eq);
};

/**
 * @description: 设置当前项
 * @param {*} eq
 * @param {*} slide    .foxui-spinner-slide
 * @return {*}
 * @Date: 2022-03-06 13:46:12
 */
Spinner.prototype._adjustActive = function (slide, eq) {
    eq = parseInt(eq.toFixed(0)); // 修复高分辨率 eq 为小数的 bug

    let $slide = $(slide),
        $container = $slide.closest(this.CONTAINER),
        $item = $slide.find(this.ITEM),
        lineNum = $container.attr('data-line');

    $item
        .removeClass('is-active')
        .eq(eq + parseInt(lineNum / 2))
        .addClass('is-active');
};

let spinner = new Spinner();

export { spinner };
